package com.ssh.common.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Vector;

public class Page implements Serializable {

	private static final long serialVersionUID = 2580248719505905102L;
	public Page(){
		this.curPageNum=1;
		//this.numberPerPage=20;
		this.pageData=new ArrayList();
	}

	// 当前所处的页数
	private long startIndex = 1L;

	// 当前所处的页数
	private long curPageNum = 1L;

	// 总共的页数
	private long totalPageNum = 0L;

	// 总共的纪录数（行数或文字数或其他）
	private long totalCount = 0L;

	// 每页所要显示的数量（记录数或文字数或其他）
	private long numberPerPage = 20L;

	// 代表页面的数据
	private List pageData;

	//模型相关的url，以/开头用在struts标签中
	private String actionUrl="";

	//上一页参数
	private HashMap priorParames=new HashMap();
	//下一页参数
	private HashMap nextParames=new HashMap();

	private HashMap  fistParamets=new HashMap();

	private HashMap lastParamets=new HashMap();

	private String url = "";

	public int getCurPageCount(){
		if(this.pageData!=null)
			return this.pageData.size();
		else
			return 0;
	}
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public HashMap getFistParamets() {
		return fistParamets;
	}

	public void setFistParamets(HashMap fistParamets) {
		this.fistParamets = fistParamets;
	}

	public HashMap getLastParamets() {
		return lastParamets;
	}

	public void setLastParamets(HashMap lastParamets) {
		this.lastParamets = lastParamets;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public String getActionUrl() {
		return actionUrl;
	}

	public void setActionUrl(String actionUrl) {
		this.actionUrl = actionUrl;
	}
	/**
	 * 如果为null将初始化一个HashMap
	 *@author 老辉迪
	 * @version $Revision: 1.3 $ $Date: 2009/07/18 06:43:58 $
	 *@since  2008-11-26 下午12:09:38
	 *@return HashMap
	 * @return
	 */
	public HashMap getNextParames() {
		if(nextParames==null)nextParames=new HashMap();
		return nextParames;
	}

	public void setNextParames(HashMap nextParames) {
		this.nextParames = nextParames;
	}

	/**
	 * 如果为null将初始化一个HashMap
	 *@author 老辉迪
	 * @version $Revision: 1.3 $ $Date: 2009/07/18 06:43:58 $
	 *@since  2008-11-26 下午12:08:33
	 *@return HashMap
	 * @return
	 */
	public HashMap getPriorParames() {
		if(priorParames==null)priorParames=new HashMap();
		return priorParames;
	}

	public void setPriorParames(HashMap priorParames) {
		this.priorParames = priorParames;
	}

	public long getCurPageNum() {
		return curPageNum;
	}

	public void setCurPageNum(long curPageNum) {
		this.curPageNum = curPageNum;
		startIndex = (curPageNum-1)*numberPerPage+1;
	}
	public void setCurPageNum(String curPageNum) {
		try{
		this.curPageNum = Integer.parseInt(curPageNum);
		startIndex = (this.curPageNum-1)*numberPerPage+1;
		}catch(Exception e){}
	}

	public long getNumberPerPage() {
		return numberPerPage;
	}

	public void setNumberPerPage(long numberPerPage) {
		this.numberPerPage = numberPerPage;
		startIndex = (curPageNum-1)*numberPerPage+1;
	}

	public List getPageData() {
		return pageData;
	}

	public void setPageData(List pageData) {
		this.pageData = pageData;
	}

	public Long getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Long totalNumberCount) {
		this.totalCount = totalNumberCount;
	}

	public long getTotalPageNum() {
		return totalPageNum;
	}

	public void setTotalPageNum(long totalPageNum) {
		this.totalPageNum = totalPageNum;
	}

	/**
	 * 是否有下一页
	 *
	 * @return
	 */
	public boolean hasNextPage() {
		return this.curPageNum < this.totalPageNum;
	}

	/**
	 * 是否有前一页
	 *
	 * @return
	 */
	public boolean hasPriorPage() {
		return this.curPageNum > 1;
	}



	public long getStartIndex() {
		return startIndex;
	}
	public void setStartIndex(long startIndex) {
		this.startIndex = startIndex;
	}

	/**
	 * 当前页数据的开始的索引，从1开始
	 * @return
	 */
	public long getCurPageStartIndex(){
		long s=1;
		s=(curPageNum-1)*numberPerPage+1;
		return s;
	}

	/**
	 * 当前页数据的结束的索引
	 * @return
	 */
	public long getCurPageEndIndex(){
		long e=1;
		e=curPageNum*numberPerPage;
		return e;
	}
	/**
	 * 一般通过查询可以查知总数量而没页要显示的数量又预先设置好 计算总页数
	 */
	public void countTotalPageNum() {

		if(numberPerPage<=0) totalPageNum=totalCount;

		if (totalCount % numberPerPage == 0) {
			totalPageNum = totalCount / numberPerPage;
		} else {
			totalPageNum = totalCount / numberPerPage + 1;
		}
	}
	/**
	 *
	 *@author 老辉迪
	 * @version $Revision: 1.3 $ $Date: 2009/07/18 06:43:58 $
	 *@since  2008-11-26 上午11:58:22
	 *@return void
	 * @param pageData 要显示的页面的数据
	 * @param totalCount 根据查询条件获取的总的数据的记录数
	 * @param actionUrl  处理分页的url路径,可以自己根据具体条件定义
	 * @param cppn       页面数的参数值
	 * @param otherParams 查询参数值
	 */
	public void initForAction(List pageData,long totalCount,String actionUrl,String cppn,HashMap otherParams){
		this.setActionUrl(actionUrl);
		this.setPageData(pageData);
		this.setTotalCount(totalCount);
		this.countTotalPageNum();
		//首页
		if(totalCount!=0)
		{
			getFistParamets().putAll(otherParams);
			getFistParamets().put(cppn,1);
		}
		//尾页
		if(totalCount!=0)
		{
			getLastParamets().putAll(otherParams);
			getLastParamets().put(cppn,totalPageNum);
		}
		//上一页
		if(hasPriorPage()){
			getPriorParames().putAll(otherParams);
			getPriorParames().put(cppn,curPageNum-1);
		}else{
			getPriorParames().clear();
		}
		//下一页
		if(hasNextPage()){
			getNextParames().putAll(otherParams);
			getNextParames().put(cppn,curPageNum+1);
		}else{
			getNextParames().clear();
		}
	}
	public void initForAction(String actionUrl,String cppn,HashMap otherParams){
		this.setActionUrl(actionUrl);
		this.setTotalCount(totalCount);
		this.countTotalPageNum();
		//首页
		if(totalCount!=0)
		{
			getFistParamets().putAll(otherParams);
			getFistParamets().put(cppn,1);
		}
		//尾页
		if(totalCount!=0)
		{
			getLastParamets().putAll(otherParams);
			getLastParamets().put(cppn,totalPageNum);
		}
		//上一页
		if(hasPriorPage()){
			getPriorParames().putAll(otherParams);
			getPriorParames().put(cppn,curPageNum-1);
		}else{
			getPriorParames().clear();
		}
		//下一页
		if(hasNextPage()){
			getNextParames().putAll(otherParams);
			getNextParames().put(cppn,curPageNum+1);
		}else{
			getNextParames().clear();
		}
	}

	public void clear(){
		curPageNum = 1;
		totalPageNum = 0;
		totalCount = 0;
		pageData.clear();
		priorParames.clear();
		nextParames.clear();
		fistParamets.clear();
		lastParamets.clear();
	}

	public static void main(String args[]){
		Page page=new Page();
		//对计算页数的测试
		page.setCurPageNum(3);
		page.setTotalCount(12L);
		page.setNumberPerPage(4);
		page.countTotalPageNum();

		System.out.println("总记录数："+page.getTotalCount());
		System.out.println("每页的纪录数："+page.getNumberPerPage());
		System.out.println("总页数："+page.getTotalPageNum());
		System.out.println("当前的页码："+page.curPageNum);
		System.out.println("是否还有下一页："+page.hasNextPage());
		System.out.println("是否还有上一页："+page.hasPriorPage());

	}
}
